<template>
	<div class="system-user-container layout-padding">
		<el-card shadow="hover" class="layout-padding-auto">
			<div class="system-user-search mb15">
				<el-space wrap>
					<div>
						<el-button text> 登记日期： </el-button>
						<el-date-picker
							v-model="date"
							type="daterange"
							range-separator="-"
							start-placeholder="选择日期"
							end-placeholder="选择日期"
							size="default"
						/>
					</div>
					<div>
						<el-button text> 需求号： </el-button>
						<el-input size="default" style="max-width: 180px" v-model="state.tableData.param.requirementNum"> </el-input>
					</div>
					<div>
						<el-button text> 需求名称： </el-button>
						<el-input size="default" style="max-width: 180px" v-model="state.tableData.param.requirementName"> </el-input>
					</div>
					<div>
						<el-button text> 服务场景码： </el-button>
						<el-input size="default" style="max-width: 180px" v-model="state.tableData.param.serviceScence"> </el-input>
					</div>
					<div>
						<el-button text> 状态： </el-button>
						<el-select v-model="status" class="m-2" size="default" clearable>
							<el-option
							v-for="item in statusOptions"
							:key="item.value"
							:label="item.label"
							:value="item.value"
							/>
						</el-select>
					</div>
					<div>
						<el-button size="default" type="primary" class="ml10" @click="getTableData()">
							<el-icon>
								<ele-Search />
							</el-icon>
							查询
						</el-button>
						<el-button size="default" type="success" class="ml10" @click="onOpenAdd('add')">
							<el-icon>
								<ele-FolderAdd />
							</el-icon>
							新增
						</el-button>
					</div>
				</el-space>
			</div>
			<el-table :data="state.tableData.data" v-loading="state.tableData.loading" style="width: 100%">
				<el-table-column type="index" label="序号" width="60" />
				<el-table-column prop="requirementNum" label="需求号" show-overflow-tooltip></el-table-column>
				<el-table-column prop="requirementName" label="需求名称" show-overflow-tooltip></el-table-column>
				<el-table-column prop="serviceScence" label="服务场景码" show-overflow-tooltip></el-table-column>
				<el-table-column prop="interfaceName" label="接口名称" show-overflow-tooltip></el-table-column>
				<el-table-column prop="oprtType" label="操作类型" show-overflow-tooltip></el-table-column>
				<el-table-column prop="revisionContent" label="修订内容" show-overflow-tooltip></el-table-column>
				<el-table-column prop="consultDoc" label="接口文档名称" show-overflow-tooltip></el-table-column>
				<el-table-column prop="developer" label="开发人员" show-overflow-tooltip></el-table-column>
				<el-table-column prop="status" label="状态" show-overflow-tooltip>
					<template #default="scope">
						<el-tag type="success" v-if="scope.row.status==0">未开发</el-tag>
						<el-tag type="success" v-else-if="scope.row.status==1">开发中</el-tag>
						<el-tag type="success" v-else-if="scope.row.status==2">开发完成</el-tag>
						<el-tag type="success" v-else-if="scope.row.status==3">上线</el-tag>
						<el-tag type="success" v-else-if="scope.row.status==4">撤销</el-tag>
						<el-tag type="danger" v-else>未知</el-tag>
					</template>
				</el-table-column>
				<el-table-column prop="createTime" label="登记时间" show-overflow-tooltip></el-table-column>
				<el-table-column label="操作" width="200">
					<template #default="scope">
						<el-button size="small" text type="primary" @click="changeStatus('finish', scope.row.id)" v-if="scope.row.status==1">开发完成</el-button>
						<el-button size="small" text type="primary" @click="changeStatus('begin', scope.row.id)" v-if="scope.row.status==0">开发</el-button>
						<el-button size="small" text type="primary" @click="onOpenEdit('edit', scope.row)">修改</el-button>
						<el-button size="small" text type="primary" @click="onRowDel(scope.row)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
			<el-pagination
				@size-change="onHandleSizeChange"
				@current-change="onHandleCurrentChange"
				class="mt15"
				:pager-count="5"
				:page-sizes="[10, 20, 30]"
				v-model:current-page="state.tableData.param.pageNum"
				background
				v-model:page-size="state.tableData.param.pageSize"
				layout="total, sizes, prev, pager, next, jumper"
				:total="state.tableData.total"
			>
			</el-pagination>
		</el-card>
		<RequirementDialog ref="requirementDialogRef" @refresh="getTableData()" />
	</div>
</template>

<script setup lang="ts" name="systemUser">
import { defineAsyncComponent, reactive, onMounted, ref } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import { requirementContentApi } from './api';

// 引入组件
const RequirementDialog = defineAsyncComponent(() => import('/@/views/daily/requirementContent/dialog.vue'));
// 引入 api 请求接口
const api = requirementContentApi();

// 定义变量内容

//日期空间默认日期为当前日期
const date = ref<[Date, Date]>([
  new Date(),
  new Date()
])

//查询条件 【状态】 的初始化
const status = ref('')
const statusOptions = [
  {
    value: '0',
    label: '未开发',
  },
  {
    value: '1',
    label: '开发中',
  },
  {
    value: '2',
    label: '开发完成',
  },
  {
    value: '3',
    label: '上线',
  },
  {
    value: '4',
    label: '撤销',
  },
]
const requirementDialogRef = ref();
const state = reactive({
	tableData: {
		data: [],
		total: 0,
		loading: false,
		param: {
			pageNum: 1,
			pageSize: 10,
			serviceScence:'',//服务场景码
			requirementName:'',//需求名称
			requirementNum:'',//需求号
			startDate:'',//起始日期
			endDate:'',//截止日期
			status:''//状态
		},
	},
});

const formatDate = (dt: Date) => {
	let year = dt.getFullYear();
    let month = (dt.getMonth() + 1).toString().padStart(2,'0');
    let date = dt.getDate().toString().padStart(2,'0');
    return `${year}-${month}-${date}`;
};

// 初始化表格数据
const getTableData = async () => {
	console.info(status.value)
	state.tableData.loading = true;
	try {
		if(date.value != null){
			state.tableData.param.startDate = formatDate(date.value[0]);
			state.tableData.param.endDate = formatDate(date.value[1]);
		}
		if((status.value != null) && (status.value != '')){
			state.tableData.param.status = status.value
		}
		const res = await api.getList(state.tableData.param);
		state.tableData.data = res.data;
		state.tableData.total = res.total;
	} catch (error:any) {
		ElMessage.warning(error.message);
	}
	state.tableData.loading = false;
};

// 修改状态
const changeStatus = (type: string, id: any) => {
	var prompt='';
	if(type == 'begin'){
		prompt='确定是你来开发这个接口吗?';
	} else {
		prompt='确定该接口已开发完成?';
	}
	ElMessageBox.confirm(prompt, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
	}).then(() => {
		api.changeStatus({'id':id,'type':type}).then(
			(res) => {
				if (res.data.code === "000000") {
					ElMessage.success(res.data.message);
					getTableData();
				} else {
					ElMessage.warning(res.data.message);
				}
			}
		);
	}).catch(() => {});
};

// 打开新增用户弹窗
const onOpenAdd = (type: string) => {
	requirementDialogRef.value.openDialog(type);
};
// 打开修改用户弹窗
const onOpenEdit = (type: string, row: any) => {
	requirementDialogRef.value.openDialog(type, row);
};
// 删除用户
const onRowDel = (row: any) => {
	ElMessageBox.confirm(`此操作将永久删除需求名称：“${row.requirementName}”的【${row.oprtType}】内容，是否继续?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
	}).then(() => {
		api.delete({'id':row.id}).then(
			(res) => {
				if (res.data.code === "000000") {
					ElMessage.success(res.data.message);
					getTableData();
				} else {
					ElMessage.warning(res.data.message);
				}
			}
		);
	}).catch(() => {});
};
// 分页改变
const onHandleSizeChange = (val: number) => {
	state.tableData.param.pageSize = val;
	getTableData();
};
// 分页改变
const onHandleCurrentChange = (val: number) => {
	state.tableData.param.pageNum = val;
	getTableData();
};
// 页面加载时
onMounted(() => {
	getTableData();
});
</script>

<style scoped lang="scss">
.system-user-container {
	:deep(.el-card__body) {
		display: flex;
		flex-direction: column;
		flex: 1;
		overflow: auto;
		.el-table {
			flex: 1;
		}
	}
}
</style>
